<html>

<style>
    * {
        margin: 0;
    }

    #content {
        color: white;
        position: absolute;
        left: -20px;
        top: -10px;
        white-space: pre;
        height: 200px;
        overflow: auto;
    }
</style>

<script type="importmap">
	{
	  "imports": {
		"three": "../../three.js-r151/build/three.module.js",
		"three/addons/": "../../three.js-r151/examples/jsm/"
	  }
	}
</script>

<body>
    <div id="content">
        1. position, scale都是 Three.Vector3类型
        2. rotation是Three.Euler类型
    </div>

    <div id="canvas"></div>

    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

        // 1. 创建三大件
        // 1.1 三维场景Scene, 类似创建了个世界，三维世界
        const scene = new THREE.Scene();
        const geometry = new THREE.BoxGeometry(1, 1, 1);
        const material = new THREE.MeshBasicMaterial({ color: 'yellow' });
        const cube = new THREE.Mesh(geometry, material);
        scene.add(cube);

        const cube1 = new THREE.Mesh(geometry, material);
        cube1.position.set(2, 0, 0)
        scene.add(cube1);

        // 添加辅助坐标系
        const axesHelper = new THREE.AxesHelper(5);
        scene.add(axesHelper);

        // 1.2 相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(5, 5, 5)

        // 1.3 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        let canvasDom = document.getElementById("canvas");
        canvasDom.appendChild(renderer.domElement);

        // 设置相机控件轨道控制器OrbitControls
        const controls = new OrbitControls(camera, canvasDom);
        // 如果OrbitControls改变了相机参数，重新调用渲染器渲染三维场景
        controls.addEventListener('change', function () {
            renderer.render(scene, camera); //执行渲染操作
        });//监听鼠标、键盘事件

        const axis = new THREE.Vector3(1, 1, 1);//向量axis

        function render() {
            cube.rotateOnAxis(axis, 0.01)
            cube1.rotateY(0.01);

            renderer.render(scene, camera);
            requestAnimationFrame(render)
        }
        render();

        // 1. new THREE.Vector3()实例化一个三维向量对象
        const v3 = new THREE.Vector3(0, 0, 0);
        console.log('v3', v3);
        v3.set(10, 0, 0);//set方法设置向量的值
        v3.x = 100;//访问x、y或z属性改变某个分量的值

        // 2. Euler: rotateX(Euler.x)
        // 创建一个欧拉对象，表示绕着xyz轴分别旋转45度，0度，90度
        const euler0 = new THREE.Euler(Math.PI / 4, 0, Math.PI / 2);
        const euler = new THREE.Euler();
        euler.x = Math.PI / 4;
        euler.y = Math.PI / 2;
        euler.z = Math.PI / 4;
        console.log("Euler", euler);


        setTimeout(() => {
            // 这样可以, 
            // position是THREE.Vector3实例
            cube1.position.set(3, 0, 0)
            cube1.position.y = 1;

            // 类型不一样, 会报错:  TypeError: Cannot assign to read only property 'position' of object '#<Mesh>'
            // cube1.position = [3, 0, 0]
        }, 3000);

    </script>
</body>

</html>